package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by c2413 on 23.06.2014.
 */
public class BuySmartphone {
    @Test
    public void testSmartphone() {
        WebDriver driver = new FirefoxDriver();
        WebDriverWait wait = new WebDriverWait(driver, 200);
        WebDriverWait wait2 = new WebDriverWait(driver, 200);

        String cartEmptyText;
        String phoneName;
        String addedPhone;
        String quantity;

        Actions builder = new Actions(driver);

        driver.get("http://rozetka.com.ua");
        cartEmptyText = driver.findElement(By.className("m-cart-empty")).getText();
        Assert.assertEquals("Корзина пуста", cartEmptyText);

        builder.moveToElement(driver.findElement(By.id("phones-mp3-gps")));
        builder.perform();
       // driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"phones-mp3-gps\"]/div/div/ol/li[1]/ul[1]/li[1]/a")));

        driver.findElement(By.xpath("//*[@id=\"phones-mp3-gps\"]/div/div/ol/li[1]/ul[1]/li[1]/a")).click();
        driver.findElement(By.xpath("//*[@id=\"head_banner_container\"]/div[4]/div/div/div[2]/div[3]/ul/li[1]/ul/li[1]/a")).click();
        driver.findElement(By.xpath("//*[@id=\"sort_view\"]/a")).click();
        driver.findElement(By.xpath("//*[@id=\"sort_view\"]/div/div/ul/li[1]/a")).click();

        phoneName = driver.findElement(By.xpath("//*[@id=\"block_with_goods\"]/div[1]/div[1]/div[1]/div[1]/div/div/div[2]/a")).getText();

        driver.findElement(By.className("to-buy-link")).click();
        driver.findElement(By.xpath("//*[@id=\"block_with_goods\"]/div[1]/div[1]/div[1]/div[1]/div/div/div[3]/div[2]/div/form/button")).click();

        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("cart-popup")));
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        addedPhone = driver.findElement(By.className("no-visited")).getText();
        quantity = driver.findElement(By.className("cart-amount-input-text")).getAttribute("value");

        Assert.assertEquals(addedPhone, phoneName);
        Assert.assertEquals(quantity, "1");

        driver.quit();
    }
}